package top.zuli.package01.class14;

/**
 * 1.线程通信的例子：使用两个线程打印 1-100。线程1, 线程2 交替打印
 * 2.方法说明:
 *      1.wait():一旦执行此方法，当前线程就进入阻塞状态，并释当前锁。
 *      2.notify():一旦执行此方法，就会唤醒被wait的一个线程。如果有多个线程被wait，就唤醒优先级高的那个。
 *      3.notifyAll():一旦执行此方法，就会唤醒所有被wait的线程。
 * 3.注意：
 *  1.wait()，notify()，notifyAll()三个方法必须使用在同步代码块或同步方法中。
 *  2.wait()，notify()，notifyAll()三个方法的调用者必须是同步代码中的同一把锁。否则，会出现IllegalMonitorStateException异常
 *  3.wait()，notify()，notifyAll()三个方法是定义在java.lang.Object类中。
 */

public class CommunicationTest {
    public static void main(String[] args) {
        Runnable runnable = new Number();
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
    }
}
class Number implements Runnable{
    private int i = 1;
    private Object obj = new Object();
    @Override
    public void run() {
        while(true){
            synchronized (obj) {
                obj.notify();
                if(i<=100){
                    System.out.println(Thread.currentThread().getName()+":"+i);
                    i++;
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    break;
                }
            }
        }
    }
}

/*
public class CommunicationTest {
    public static void main(String[] args) {
        Runnable runnable = new Number();
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
    }
}
class Number implements Runnable{
    private int i = 1;
    private Object obj = new Object();
    @Override
    public void run() {
        while(true){
            synchronized (obj) {
                obj.notify();
                if(i<=100){
                    System.out.println(Thread.currentThread().getName()+":"+i);
                    i++;
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    break;
                }
            }
        }
    }
}
*/

/*
public class CommunicationTest {
    public static void main(String[] args) {
        Runnable runnable = new Number();
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
    }
}
class Number implements Runnable{
    private int i = 1;
    private Object obj = new Object();
    @Override
    public void run() {
        while(true){
            synchronized (obj) {
                obj.notify();
                if(i<=100){
                    System.out.println(Thread.currentThread().getName()+":"+i);
                    i++;
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    break;
                }
            }
        }
    }
}
*/
